﻿using AutoMapper;
using MediatR;
using Production_Set.API.Application.Commands.Customerser;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.Produce.Customers;
using THE_WIND_RISES.Common.Domans.Produce.Order;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Set.API.Application.Commands.Orderers
{
    public class OrdersCreateHandler : IRequestHandler<OrdersCreateCommend, Result>
    {
        private readonly IBaseRepository<Orders> Orders;
        private readonly IMapper mapper;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="Resource"></param>
        public OrdersCreateHandler(IMapper mapper, IBaseRepository<Orders> Orders)
        {
            this.mapper = mapper;
            this.Orders = Orders;
        }

        public async Task<Result> Handle(OrdersCreateCommend request, CancellationToken cancellationToken)
        {
            var results = new Result() { Code = (int)CommonErrorCodes.Added };

            var AddData = mapper.Map<Orders>(request);
            string sql = "insert into Orders values(@OrdersId,@OrdersNo,@OrdersName,@OrdersTypeId,@ProjectId,@OrdersRemark,@CustomerId,@OrdersPrice,@Currency,@PrePrice,@OrdersKTime,@OrdersJTime,@SignedTime,@OrdersState," +
                "@TaxType,@Rate,@BillingPrice,@CollectedPrice,@Salesman,@DepartmentId,@ApprovalStatusDD,@IsDelete,@Handle,@HandleContext,@HandleTime,@Pid);";
            var AddNum = Orders.Add(sql, new
            {
                OrdersId = AddData.OrdersId,
                OrdersNo = AddData.OrdersNo,
                OrdersName = AddData.OrdersName,
                OrdersTypeId = AddData.OrdersTypeId,
                ProjectId = AddData.ProjectId,
                OrdersRemark = AddData.OrdersRemark,
                CustomerId = AddData.CustomerId,
                OrdersPrice = request.OrdersPrice,
                Currency = request.Currency,
                PrePrice = request.PrePrice,
                OrdersKTime = request.OrdersKTime,
                OrdersJTime = AddData.OrdersJTime,
                SignedTime = AddData.SignedTime,
                OrdersState = AddData.OrdersState,
                TaxType = AddData.TaxType,
                Rate = AddData.Rate,
                BillingPrice = AddData.BillingPrice,
                CollectedPrice = AddData.CollectedPrice,
                Salesman = AddData.Salesman,
                DepartmentId = AddData.DepartmentId,
                ApprovalStatusDD = AddData.ApprovalStatusDD,
                IsDelete = AddData.IsDelete,
                Handle = AddData.Handle = "李四",
                HandleContext = AddData.HandleContext="添加",
                HandleTime = AddData.HandleTime= DateTime.Now,
                Pid = AddData.Pid=0,
            });
            if (AddNum > 0)
            {
                results.Code = (int)CommonErrorCodes.Success;
                results.Msg = "添加成功";
                return results;
            }
            else
            {
                return results;
            }
        }
    }
}
